package com.puman.watchtrade.fragment.home.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.home.HomeFragment;
import com.puman.watchtrade.fragment.home.model.HomeItems;
import com.puman.watchtrade.util.Gjfun;
import com.puman.watchtrade.util.GlobalData;

/**
 * 相片适配器
 * 
 * @author GuiLin
 */
public class HomeItemGridViewAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ViewHolder viewHolder;
	private GridView gridView;
	private HomeItems homeItems = new HomeItems();
	private HomeFragment homeFragment;
	private Context context;

	public HomeItemGridViewAdapter(Context context, HomeFragment homeFragment,
			HomeItems homeItems) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.homeItems = homeItems;
		this.homeFragment = homeFragment;
	}

	@Override
	public int getCount() {
		return homeItems.homeItemList.size();
	}

	@Override
	public Object getItem(int position) {
		return homeItems.homeItemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * 刷新view
	 * 
	 * @param index
	 */
	public void refreshView(int index) {
		int visiblePos = gridView.getFirstVisiblePosition();
		View view = gridView.getChildAt(index - visiblePos);
		ViewHolder holder = (ViewHolder) view.getTag();

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_home, null);
			viewHolder.image = (ImageView) convertView
					.findViewById(R.id.home_item_image);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		// 设置透明度
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		// 设置动画执行的时间
		alphaAnimation.setDuration(1000);

		// 从上网下 带透明
		AnimationSet animationSetFromeBelow = new AnimationSet(true);
//		TranslateAnimation translateAnimationFromeBelow = new TranslateAnimation(
//				Animation.RELATIVE_TO_SELF, 0f,// 参数1～2：x轴的开始位置
//				Animation.RELATIVE_TO_SELF, 0f,// 参数5～6：x轴的结束位置
//				Animation.RELATIVE_TO_SELF, 1.5f,// 参数3～4：y轴的开始位置
//				Animation.RELATIVE_TO_SELF, 0f); // 参数7～8：y轴的结束位置

//		translateAnimationFromeBelow.setDuration(1200);
		animationSetFromeBelow.addAnimation(alphaAnimation);
//		animationSetFromeBelow.addAnimation(translateAnimationFromeBelow);
		
		viewHolder.image.startAnimation(animationSetFromeBelow);

		LayoutParams layoutParams = (LayoutParams) viewHolder.image
				.getLayoutParams();
		layoutParams.height = (GlobalData.screenHeight - Gjfun.dip2px(
				MainActivity.getInstant(), 50))
				/ 2
				- Gjfun.dip2px(MainActivity.getInstant(), 25);
		layoutParams.width = GlobalData.screenWidth / 2
				- Gjfun.dip2px(MainActivity.getInstant(), 15);

		// Log.i("curry", "height:"+(GlobalData.screenHeight -
		// Gjfun.dip2px(MainActivity.getInstant(), 50)));
		// Log.i("curry", "width:"+layoutParams.height);
		// Log.i("curry", "width:"+layoutParams.width);
		viewHolder.image.setLayoutParams(layoutParams);
		viewHolder.image.setImageResource(homeItems.homeItemList.get(position)
				.getImgUrl());
		// 如果有头像，载入，否则显示layout上面的（@drawable/home_new_icon）
		// Log.d("HeadPicture", GlobalData.userInfo.headPicture.trim());
		// CheckImageLoaderConfiguration.checkImageLoaderConfiguration(context);
		// UniversalImageLoadTool.display(homeItems.homeItemList.get(position).getImgUrl(),
		// (ImageView)viewHolder.image, R.drawable.icon);
		return convertView;
	}

	public class ViewHolder {
		public ImageView image;
	}
}
